<template>
  <div style="padding: 20px;">
    <!-- 操作栏 -->
    <el-card style="margin-bottom: 20px;">
      <div style="display: flex; justify-content: space-between; align-items: center;">
        <el-button type="success" @click="handleAdd">新增</el-button>
        <el-button @click="handleRefresh">刷新</el-button>
      </div>
    </el-card>

    <!-- 数据表格 -->
    <el-card>
      <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="index" label="序号" width="80" />
        <el-table-column prop="inboundCode" label="入库单号" width="150" />
        <el-table-column prop="inboundTypeName" label="入库类型" width="120" />
        <el-table-column prop="depotId" label="仓库编号" width="100" />
        <el-table-column prop="depotName" label="仓库名称" width="120" />
        <el-table-column prop="inboundTime" label="入库日期" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.inboundTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="inboundRemark" label="备注" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div style="margin-top: 20px; display: flex; justify-content: space-between; align-items: center;">
        <span>共 {{ totalCount }} 条</span>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 15, 20, 50]"
          :total="totalCount"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { http } from '@/axios/axiosHelpers'

const router = useRouter()
const loading = ref(false)
const tableData = ref([])
const totalCount = ref(0)
const currentPage = ref(1)
const pageSize = ref(15)
const selectedRows = ref([])

// 获取数据
const fetchData = async () => {
  loading.value = true
  try {
    const response = await http.get('/basicwrite/Inbound/GetInbound', {
      params: {
        index: currentPage.value,
        size: pageSize.value
      }
    })
    
    if (response.data) {
      tableData.value = response.data.data || []
      totalCount.value = response.data.totalCount || 0
      
      // 添加序号
      tableData.value.forEach((item, index) => {
        item.index = (currentPage.value - 1) * pageSize.value + index + 1
      })
    }
  } catch (error) {
    console.error('获取数据失败:', error)
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

// 格式化日期
const formatDate = (dateString: string) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 新增
const handleAdd = () => {
  router.push('/Inbound')
}

// 刷新
const handleRefresh = () => {
  fetchData()
}

// 编辑
const handleEdit = (row: any) => {
  router.push(`/inbound/edit/${row.inboundId}`)
}

// 删除
const handleDelete = async (row: any) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除入库单号 ${row.inboundCode} 吗？`,
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // 这里可以调用删除接口
    // await axios.delete(`http://localhost:5011/api/Inbound/DeleteInbound/${row.inboundId}`)
    
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

// 选择变化
const handleSelectionChange = (selection: any[]) => {
  selectedRows.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pageSize.value = size
  currentPage.value = 1
  fetchData()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  currentPage.value = page
  fetchData()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.el-card {
  margin-bottom: 20px;
}

.el-table {
  margin-top: 10px;
}
</style>